<?php
require_once("controllers/init.php");
require_once("model/classes/events/EventsResultSet.php");


function output_string_protected($string) 
{
    return htmlspecialchars(trim($string));
}  

header('Content-type: text/calendar; charset=utf-8');
header("Content-Disposition: inline; filename=snap.ics");

/*require_once('../../archived old website/includes/class.database.php');
require_once('../../archived old website/includes/xml.php');*/

/*$Database = Database::connect('localhost', 'portseap', 'ppsite21');
$Database->selectDatabase('admin_portseap');
$Qdata = $Database->query('select e.EventID, e.Date, e.Start, e.End, e.CreateTime, e.DeletedTime, e.Title, e.Description, l.Location from :table_events e left join :table_locations l on (e.LocationID = l.LocationID) where e.Date >= CURDATE() and e.DeletedUserID = 0 order by e.Date, e.Start');
$Qdata->bindTable(':table_events', 'Events');
$Qdata->bindTable(':table_locations', 'Locations');
$Qdata->execute();*/

echo "BEGIN:VCALENDAR\n";
echo "METHOD:PUBLISH\n";
echo "PRODID:SNAP\n";
echo "VERSION:2.0\n";

echo "BEGIN:VTIMEZONE\n";
echo "TZID:Europe/London\n";
echo "BEGIN:DAYLIGHT\n";
echo "TZOFFSETFROM:+0000\n";
echo "TZOFFSETTO:+0100\n";
echo "DTSTART:19810329T010000\n";
echo "RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\n";
echo "TZNAME:BSTr\n";
echo "END:DAYLIGHT\n";
echo "BEGIN:STANDARD\n";
echo "TZOFFSETFROM:+0100\n";
echo "TZOFFSETTO:+0000\n";
echo "DTSTART:19961027T020000\n";
echo "RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\n";
echo "TZNAME:GMT\n";
echo "END:STANDARD\n";
echo "END:VTIMEZONE\n";

/* while ($Qdata->next()) {
$Qeng = $Database->query('select ng.NotifyGroup from :table_notifyeventgroups neg left join :table_notifygroups ng using (NotifyGroupID) where neg.EventID = :EventID');
$Qeng->bindTable(':table_notifyeventgroups', 'EventNotifyGroups');
$Qeng->bindTable(':table_notifygroups', 'NotifyGroups');
$Qeng->bindInt(':EventID', $Qdata->valueInt('EventID'));
$Qeng->execute();
while ($Qeng->next()) {
$ngroup = $Qeng->value('NotifyGroup');
}
*/

$results = new EventsResultSet();
$results->setFrom(new DateTime("2012-11-01"));
$results->setTo(new DateTime("3000-01-01"));
$events = $results->getEvents();

//print_r($events)

foreach($events as $event)

{
	echo "BEGIN:VEVENT\n";
	echo "CLASS:PUBLIC\n";
	echo "UID:" . md5(uniqid(mt_rand(), true)) . "@stnicholascathedralmusic.co.uk\n";
	echo "CREATED:" . $event->getCreatedWhen()->format('Ymd\THis') . "\n";
	echo "SUMMARY:" . $event->getTitle() . "\n";
	$description = $event->getDescription()." - ".$event->getComment()." [".$event->getInvolveesList()."] [".$event->getLocationsList()."]";
	$description = str_ireplace('<br>', ', ', $description);
	echo "DESCRIPTION:".$description."\n";
	$timenow = new DateTime('now');
	echo "DTSTAMP:" . $timenow->format('Ymd\THis') . "\n";
	echo "DTEND;TZID=Europe/London:" . $event->getEnd()->format('Ymd\THis') . "\n";
	echo "DTSTART;TZID=Europe/London:" . $event->getStart()->format('Ymd\THis') . "\n";
//	echo "LOCATION:" . $Qdata->valueProtected('Location') . "\n";
	echo "END:VEVENT\n";
}

echo "END:VCALENDAR";
 ?>